<?php
/**
 * Geneone: Content Management Framework
 * Base Object View
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
/* All object views inherit from this */
class Gene_Object_View {	
	/**
	 * Redirect to parent upon successful create/modify
	 */
	var $redirectParent = false;
	
	function view($actions=array()) {}
	
	/**
	 * Displays the edit form for the object.
	 *
	 * @return string HTML output for editing object.
	 */
	function edit($actions=array()) {
		$tpl =& Gene::getTemplate();
		$user =& Gene::getUser();

		/* General Object Information */
		$info =& $this->model->get();
		
		$tpl->assign ("pagetitle", $info['title']);
		$tpl->assignRef ("object", $info);

		/* Parents */
		$parents = $this->model->getParents();
		
		$tpl->assign ("parents", $parents);
		
		$tpl->assign("curlbase", Gene::makeLink(Gene::objectID($info['object_id'])));
		
		/* Find Allowed Actions */
		$this->editActions = array(
			"content" => "Page Content",
		);
		
		$this->getCustomEditActions();
		
		if ($this->model->canModerate()) {
			$this->editActions['advanced'] = "Advanced";
			
			if ($info['parent'] != "-1" && $user->hasPermission("namespaces.object.remove.".$info['type'])) {
				$this->editActions['remove'] = "Delete";
			}
		}
		
		if (isset($actions[2]) && isset($this->editActions[$actions[2]])) {
			$action = $actions[2];
		} else {
			$action = 'content';
		}
		
		/* Process Action */
		switch ($action) {
			case 'content':
				$this->callContent($actions);
				break;
			case 'advanced':
				$this->callAdvanced($actions);
				break;
			case 'remove':
				$this->callRemove($actions);
				break;
			default:
				$tpl->assign ("editcontent", $this->callCustomEditAction($action));
		}
		
		$tpl->assignRef("editactions", $this->editActions);
		$tpl->assign ("action", $action);

		return $tpl->fetch("objects/edit");
	}
	
	/**
	 * Call content edit page
	 */
	function callContent($actions=array()) {
		$tpl =& Gene::getTemplate();
		
		$info = $this->model->get();
		
		if ($_SERVER['REQUEST_METHOD'] == "POST") {
			$title = Gene_Request::getString("objtitle", GENE_REQUEST_POST);
			$desc = Gene_Request::getString("objdesc", GENE_REQUEST_POST);
			$content = Gene_Request::getString("objcontent", GENE_REQUEST_POST);
			
			$rev_summary = Gene_Request::getString("rev_summary", GENE_REQUEST_POST);
			$rev_minor = Gene_Request::getString("rev_minor", GENE_REQUEST_POST) ? true : false;
			
			// What we are editing
			$options = array(
				"title" => $title,
				"description" => $desc,
				"content" => $content,
				"revision_summary" => $rev_summary,
				"revision_minor" => $rev_minor,
			);

			// Edit
			$edit = $this->doEdit($options);
			if (PEAR::isError($edit)) {
				$tpl->assign("errormsg", $edit->message);
			} else {
				if ($this->redirectParent) {
					header ("Location: ".Gene::makeLink($info['parent']));
				} else {
					header ("Location: ".Gene::makeLink($info));
				}
				exit();
			}
		}
		
		// Load a revision?
		if ($this->model->canRevisions()) {
			$revision = Gene_Request::getInteger("revision", GENE_REQUEST_GET);
			if ($revision && $r = $this->model->getRevision($revision)) {
				$info['title'] = $r['title'];
				$info['description'] = $r['description'];
				$info['content'] = $r['content'];
				
				$tpl->assign ("object", $info);
				$tpl->assign ("revision", $r);
			}
		}
		
		$tpl->assign ("editcontent", $tpl->fetch("objects/".$info['type']."/edit/content"));
	}
	
	/**
	 * Call advanced edit page.
	 */
	function callAdvanced($actions=array()) {
		$tpl =& Gene::getTemplate();
		$user =& Gene::getUser();
		
		$info = $this->model->get();
		$tpl->assign ("ownername", $info['name']);
		
		if (isset($actions[3]) && $_SERVER['REQUEST_METHOD'] == "POST") {
			if ($actions[3] == "alias" && $user->hasPermission("namespaces.object.set_aliases")) {
				$newalias = Gene_Request::getString("alias", GENE_REQUEST_POST);
				$newalias = str_replace (" ", "_", $newalias); // Remove spaces
				
				if (Gene_Object_Model::isValidAlias($newalias)) {
					if ($this->model->setAlias($newalias)) {
						$tpl->assign ("aliassuccess", true);
					} else {
						$tpl->assign ("aliaserror", "The alias you specified is already being used.");
					}
				} else {
					$tpl->assign ("aliaserror", "The alias you specified contains invalid characters.");
				}
			} else if ($actions[3] == "permissions" && $user->hasPermission("namespaces.object.permissions")) {
				$permissions = array(
					"view" => Gene_Request::getString("p_view", GENE_REQUEST_POST),
					"edit" => Gene_Request::getString("p_edit", GENE_REQUEST_POST),
					"revisions" => Gene_Request::getString("p_revisions", GENE_REQUEST_POST),
					"write" => Gene_Request::getString("p_write", GENE_REQUEST_POST),
					"moderate" => Gene_Request::getString("p_moderate", GENE_REQUEST_POST),
				);
				
				$this->model->setPermissions($permissions);
				$tpl->assign ("permissionsuccess", true);
			} else if ($actions[3] == "properties" && $user->hasPermission("namespaces.object.properties")) {
				$new = Gene_Request::getBoolean("new", GENE_REQUEST_POST);
				if ($new) {
					$name = Gene_Request::getString("propertyname", GENE_REQUEST_POST);
					$value = Gene_Request::getString("propertyvalue", GENE_REQUEST_POST);
					if (!$name) {
						$tpl->assign ("propertieserror", "You did not provide a property name.");
					} else if ($this->model->getProperty($name)) {
						$tpl->assign ("propertieserror", "This property already exists.");
					} else {
						$this->model->setProperty($name, $value);
					}
				} else {
					$properties = Gene_Request::getArray("properties", GENE_REQUEST_POST);
					$delete = Gene_Request::getArray("delete", GENE_REQUEST_POST);
					
					if (is_array($delete) && count($delete)) {
						foreach ($delete as $i) {
							if (isset($properties[$i])) {
								$properties[$i] = '';
							}
						}
					}
					
					foreach ($properties as $j => $k) {
						$this->model->setProperty($j, $k);
					}
					
					$tpl->assign ("propertiessuccess", true);
				}
			} else if ($actions[3] == "owner" && $user->hasPermission("namespaces.object.change_owner")) {
				$ownername = Gene_Request::getString("name", GENE_REQUEST_POST);
				if ($newuser = Gene_User::fromPublicName($ownername)) {
					$member = $newuser->getInfo();
					$this->model->setOwner($member['user_id']);
					$tpl->assign ("ownername", $member['name']);
					$tpl->assign ("ownersuccess", true);
				} else {
					$tpl->assign ("ownererror", "The specified member does not seem to exist.");
				}
			}
		}
		
		$tpl->assign ("properties", $this->model->listProperties());
		$tpl->assign ("editcontent", $tpl->fetch("objects/edit/advanced"));
	}
	
	/**
	 * Call remove page.
	 */
	function callRemove($actions=array()) {
		$tpl =& Gene::getTemplate();
		
		$confirm = Gene_Request::getBoolean("confirm", GENE_REQUEST_POST);
		if ($_SERVER['REQUEST_METHOD'] == "POST" && $confirm) {
			$info = $this->model->get();
			
			$this->model->remove();
			header ("Location: ".Gene::makeLink($info['parent']));
			exit();
		}
		
		$tpl->assign ("editcontent", $tpl->fetch("objects/edit/remove"));
	}
	
	/**
	 * Callback function to specify object specific edit actions.
	 */
	function getCustomEditActions() {}
	
	/**
	 * Callback function to call specific edit action
	 */
	function callCustomEditAction($action) {}
	
	function create() {}
	
	/**
	 * Creates the new object.
	 * 
	 * Objects can implement their own creation routines here to fiddle
	 * the user input before sending to Gene_Object::create() or return a
	 * PEAR::raiseError if input is invalid
	 */
	function doCreate($type, $title='', $desc='', $content='', &$parentobj, $options=array(), $draft=false) {
		if ($draft) {
			require_once 'Gene/Object/Draft.php';
			if (is_integer($draft)) {
				$update = $draft;
			} else {
				$update = false;
			}
			
			return Gene_Object_Draft::save($type, $title, $desc, $content, $parentobj, $options, $update);
		} else {
			return Gene_Object::create($type, $title, $desc, $content, $parentobj, $options);
		}
	}
	
	/**
	 * Edits the current object.
	 *
	 * Objects can implement callback routines here.
	 */
	function doEdit($options=array()) {
		return $this->model->setContent($options);
	}
	
	/**
	 * Calls web services
	 *
	 * Objects should implement XHR/JSON callback routines here.
	 */
	function callWebServices($method) {}
}
?>